import requests
from exception import ThreatNameError

url = "http://sre-api.gist.symantec.com/"

class ThreatName:
    def __init__(self, md5):       
        self.md5 = md5
        self.req = self._requestGuess()
        self.threatList = self._getThreatList()
        
    # handles the request for threat guesses for an MD5
    # {u'result': [{u'Threat': [{u'Score': 6593, u'Threat': .... }
    def _requestGuess(self):
        try:
            req = requests.get( url + "guess/" + self.md5 + ".json")
            guess = req.json()
            return guess      
        except ValueError:
            raise ThreatNameError(self.md5 + " produces bad Threat Guesses Request")

    # get the Threat Names of an MD5
    # returns as list of Threat names  
    def _getThreatList(self):
        guessList = []
        for item in self.req['result'][0]['Threat']:
            guessList.append(str(item['Threat']))
        return guessList
    
    # compares the list of threat names of two Md5s
    # takes as parameter the list of Threat names of other Md5
    # returns as list the similarity between two list of Threat names 
    def compareThreatGuesses(self, threatList_other):
        similarity = list(set(self.threatList) & set(threatList_other))
        return similarity